package cn.bocweb.spinning.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import java.util.List;
import java.util.Map;

import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.AddressSelectBean;
import cn.bocweb.spinning.model.Bean.CompanySearchLocationBean;

/**
 * Created by chenchangxing on 15/11/19.
 */
public class ProvinceAdapter extends BaseExpandableListAdapter {

    Map<String, List<CompanySearchLocationBean.ProvinceBean.CityBean>> map;
    List<AddressSelectBean.Erea> cityList;
    Context context;
    LayoutInflater inflater;
    List<CompanySearchLocationBean.ProvinceBean> provinceList;

    public ProvinceAdapter(Map<String, List<CompanySearchLocationBean.ProvinceBean.CityBean>> map , Context context, List<CompanySearchLocationBean.ProvinceBean> provinceList) {
        this.map = map;
        this.context = context;
        this.provinceList = provinceList;
        this.inflater = LayoutInflater.from(context);

    }

    @Override
    public int getGroupCount() {
        return provinceList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        CompanySearchLocationBean.ProvinceBean key = provinceList.get(groupPosition);
        return map.get(key.getTitle()).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        CompanySearchLocationBean.ProvinceBean province = provinceList.get(groupPosition);
        return province.getTitle();
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        CompanySearchLocationBean.ProvinceBean key = provinceList.get(groupPosition);
        return map.get(key.getTitle()).get(childPosition).getTitle();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_province, null);
            viewHolder = new GroupViewHolder();
            viewHolder.tv_province = (TextView)convertView.findViewById(R.id.tv_province);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (GroupViewHolder)convertView.getTag();
        }

        viewHolder.tv_province.setText(provinceList.get(groupPosition).getTitle());

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_city, null);
            viewHolder = new ChildViewHolder();
            viewHolder.tv_city = (TextView)convertView.findViewById(R.id.tv_city);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ChildViewHolder)convertView.getTag();
        }

        viewHolder.tv_city.setText(map.get(provinceList.get(groupPosition).getTitle()).get(childPosition).getTitle());

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private class GroupViewHolder {
        TextView tv_province;
    }

    private class ChildViewHolder {
        TextView tv_city;
    }
}
